package cn.ac.zing.server.hj212.handler;

import cn.ac.zing.api.enums.ProtocolTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：dcy
 * @name：ProtocolTypeHandlerFactory
 * @Date：2024/5/21 20:42
 */
@Component
public class ProtocolTypeHandlerFactory  implements InitializingBean {
    @Resource
    private List<ProtocolTypeHandler> protocolTypeHandlerList;

    private Map<ProtocolTypeEnum, ProtocolTypeHandler> handlerMap = new HashMap<>();

    public ProtocolTypeHandler getHandler(int protocolType) {

        ProtocolTypeEnum subjectInfoTypeEnum = ProtocolTypeEnum.getByCode(protocolType);

        return handlerMap.get(subjectInfoTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (ProtocolTypeHandler protocolTypeHandler : protocolTypeHandlerList) {

            handlerMap.put(protocolTypeHandler.getHandlerType(), protocolTypeHandler);
        }
    }
}
